package com.tg.demo.common.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import org.springframework.validation.BindingResult;

/**
 * 通用返回对象
 */
@Data
public class CommonResult {
    //操作成功
    public static final int SUCCESS = 200;
    //操作失败
    public static final int FAILED = 500;
    //参数校验失败
    public static final int VALIDATE_FAILED = 404;
    //未认证
    public static final int UNAUTHORIZED = 401;
    //未授权
    public static final int FORBIDDEN = 403;
    private int code;
    private String msg;
    private Object data;

    /**
     * 普通成功返回
     *
     * @param data 获取的数据
     */
    public static CommonResult success(Object data) {
        CommonResult instance = new CommonResult();
        instance.code = SUCCESS;
        instance.msg = "操作成功";

        if(data instanceof IPage){
            instance.data = getPageResult((IPage) data);
        }else {
            instance.data = data;
        }
        return instance;
    }
    /**
     * 普通成功返回
     *
     */
    public static CommonResult success() {
        CommonResult instance = new CommonResult();
        instance.code = SUCCESS;
        instance.msg = "操作成功";
        instance.data =  "操作成功";
        return instance;
    }
    /**
     * 普通成功返回
     */
    public static CommonResult success(String msg, Object data) {
        CommonResult instance = new CommonResult();
        instance.code = SUCCESS;
        instance.msg = msg;
        instance.data = data;
        return instance;
    }


    /**
     * 普通失败提示信息
     */
    public static CommonResult failed() {
        CommonResult instance = new CommonResult();
        instance.code = FAILED;
        instance.msg = "操作失败";
        return instance;
    }

    public static CommonResult failed(String msg) {
        CommonResult instance = new CommonResult();
        instance.code = FAILED;
        instance.msg = msg;
        return instance;
    }

    /**
     * 参数验证失败使用
     *
     * @param msg 错误信息
     */
    public static CommonResult validateFailed(String msg) {
        CommonResult instance = new CommonResult();
        instance.code = VALIDATE_FAILED;
        instance.msg = msg;
        return instance;
    }

    /**
     * 未登录时使用
     *
     * @param msg 错误信息
     */
    public static CommonResult unauthorized(String msg) {
        CommonResult instance = new CommonResult();
        instance.code = UNAUTHORIZED;
        instance.msg = "暂未登录或token已经过期";
        instance.data = msg;
        return instance;
    }

    /**
     * 未授权时使用
     *
     * @param msg 错误信息
     */
    public static CommonResult forbidden(String msg) {
        CommonResult instance = new CommonResult();
        instance.code = FORBIDDEN;
        instance.msg = "没有相关权限";
        instance.data = msg;
        return instance;
    }

    /**
     * 参数验证失败使用
     *
     * @param result 错误信息
     */
    public static CommonResult validateFailed(BindingResult result) {
        return validateFailed(result.getFieldError().getDefaultMessage());
    }
    /**
     * 普通失败提示信息
     */
    public static CommonResult paramFailed() {
        CommonResult instance = new CommonResult();
        instance.code = FAILED;
        instance.msg = "参数失败";
        return instance;
    }

    public static CommonResult paramFailed(String msg) {
        CommonResult instance = new CommonResult();
        instance.code = FAILED;
        instance.msg = msg;
        return instance;
    }


    /**
     * sql 执行结果
     * @param success
     * @return
     */
    public static CommonResult sqlExecResult(Boolean success) {
        return success ? success():failed();
    }

    /**
     * sql 执行结果
     * @param count 返回结果数量
     * @return
     */
    public static CommonResult sqlExecResult(int count) {
        return count>0 ? success():failed();
    }

    private static PageResult getPageResult(IPage page){
        PageResult pageResult = new PageResult();
        pageResult.setPageSize(page.getSize());
        pageResult.setPageNo(page.getCurrent());
        pageResult.setTotalPage(page.getPages());
        pageResult.setTotalCount(page.getTotal());
        pageResult.setData(page.getRecords());
        return pageResult;
    }
}
